package cn.gailvlun.gll.presentation.emotion.edit;

import android.support.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import java.util.ArrayList;
import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityEmotionRecordBinding;
import cn.gailvlun.gll.net.emotion.EmotionEnum;
import cn.gailvlun.gll.net.emotion.EmotionRecordReq;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.SPUtil;
import cn.gailvlun.gll.widget.photopicker.PhotoItem;



public class DraftDialog {


    private final String PREF_EMOTION_SAVE_DRAFT = "pref_emotion_save_draft";

    static class Draft {
        private static Draft INSTANCE = new Draft();

        public static Draft getInstance() {
            return INSTANCE;
        }

        EmotionEnum emotionEnum;
        String text;
        List<PhotoItem> photoItemList = new ArrayList<>();
    }

    private static DraftDialog INSTANCE = null;

    public static DraftDialog getInstance() {
        if (INSTANCE == null) {
            return new DraftDialog();
        }
        return INSTANCE;
    }

    void saveDraft(EmotionEnum emotionEnum, String text, List<PhotoItem> photoItemList) {
        Draft draft = Draft.getInstance();
        draft.text = text;
        draft.photoItemList.clear();
        draft.photoItemList.addAll(photoItemList);
        draft.emotionEnum = emotionEnum;
        SPUtil.put(SPUtil.USER, PREF_EMOTION_SAVE_DRAFT, draft);

    }

    void clearDraft() {
        SPUtil.put(SPUtil.USER, PREF_EMOTION_SAVE_DRAFT, null);
    }

    Draft restoreDraft(ActivityEmotionRecordBinding binding) {
        Draft draft = SPUtil.get(SPUtil.USER, PREF_EMOTION_SAVE_DRAFT, Draft.class);
        if (draft != null) {
            EmotionEnum emotionEnum = draft.emotionEnum;
            List<PhotoItem> photoItemList = draft.photoItemList;
            String text = draft.text;
            if (emotionEnum != null) {
                binding.navigationBar.setMenuEnable(true);
                binding.navigationBar.setMenuTextColor(true ? R.color.apricot : R.color.pinkishGrey);
                switch (emotionEnum) {
                    case S:
                        binding.rbHappy.setChecked(true);
                        break;
                    case T:
                        binding.rbLaughCry.setChecked(true);
                        break;
                    case F:
                        binding.rbSad.setChecked(true);
                        break;
                    case A:
                        binding.rbAngry.setChecked(true);
                        break;
                    case N:
                    case X:
                    default:
                        binding.navigationBar.setMenuEnable(false);
                        binding.navigationBar.setMenuTextColor(false ? R.color.apricot : R.color.pinkishGrey);

                }
            }
            if (!IsEmpty.string(text)) {
                binding.cevContent.setText(text);
            }

            if (!IsEmpty.list(photoItemList)) {
                binding.photoPicker.setPhotoItems(photoItemList);
            }
        }

        return draft;
    }

    EmotionRecordReq draft2Req(Draft draft) {
        EmotionRecordReq req = new EmotionRecordReq();
        if (draft != null) {

            req.setText(draft.text);
            req.setPics(PhotoItem.transform2oss(draft.photoItemList));
        }
        return req;
    }

    
    void showIfNeed(final BaseActivity activity, final EmotionEnum emotionEnum, final String text, final List<PhotoItem> photoItemList) {

        if (!IsEmpty.string(text) || !IsEmpty.list(photoItemList)) {
            new MaterialDialog.Builder(activity)
                    .title("将此次编辑保留")
                    .positiveText("保留")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                            saveDraft(emotionEnum,
                                    text,
                                    photoItemList);


                            activity.finish();

                        }
                    })
                    .negativeText("不保留")
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                            clearDraft();
                            activity.finish();
                        }
                    }).show();
        } else {
            
            clearDraft();
            activity.finish();
        }

    }

}
